Write-Host "启动所有 API 项目..." -ForegroundColor Green

# 检查项目是否存在
$projects = @(
    "MediPlatform.Read.Api/MediPlatform.Read.Api.csproj",
    "MediPlatform.Write.Api/MediPlatform.Write.Api.csproj", 
    "SmartMedicalRAG/src/SmartMedicalRAG.Api/SmartMedicalRAG.Api.csproj"
)

foreach ($project in $projects) {
    if (-not (Test-Path $project)) {
        Write-Host "错误：找不到项目文件 $project" -ForegroundColor Red
        exit 1
    }
}

# 清理所有项目
Write-Host "清理所有项目..." -ForegroundColor Yellow
dotnet clean MyMediPlatform.sln

# 还原包
Write-Host "还原包..." -ForegroundColor Yellow
dotnet restore MyMediPlatform.sln

# 构建解决方案
Write-Host "构建解决方案..." -ForegroundColor Yellow
dotnet build MyMediPlatform.sln --verbosity minimal

# 检查构建是否成功
if ($LASTEXITCODE -ne 0) {
    Write-Host "构建失败！请检查错误信息。" -ForegroundColor Red
    exit 1
}

Write-Host "构建成功！" -ForegroundColor Green

# 检查端口是否被占用
Write-Host "检查端口占用情况..." -ForegroundColor Yellow
$ports = @(5000, 5001, 56704, 56705)
foreach ($port in $ports) {
    $process = netstat -ano | findstr ":$port "
    if ($process) {
        Write-Host "警告：端口 $port 可能被占用" -ForegroundColor Yellow
    }
}

Write-Host ""
Write-Host "准备启动三个 API 项目：" -ForegroundColor Cyan
Write-Host "  1. MediPlatform.Read.Api" -ForegroundColor Cyan
Write-Host "  2. MediPlatform.Write.Api" -ForegroundColor Cyan  
Write-Host "  3. SmartMedicalRAG.Api" -ForegroundColor Cyan
Write-Host ""
Write-Host "请确保以下服务已启动：" -ForegroundColor Yellow
Write-Host "  - 数据库服务 (SQL Server/MySQL)" -ForegroundColor Yellow
Write-Host "  - Ollama 服务 (如果使用)" -ForegroundColor Yellow
Write-Host ""
Write-Host "按任意键开始启动..." -ForegroundColor Green
Read-Host

# 启动所有项目（使用多个终端窗口）
Write-Host "正在启动 API 项目..." -ForegroundColor Green

# 启动 MediPlatform.Read.Api
Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$PWD'; dotnet run --project MediPlatform.Read.Api/MediPlatform.Read.Api.csproj"

# 等待一下再启动下一个
Start-Sleep -Seconds 2

# 启动 MediPlatform.Write.Api  
Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$PWD'; dotnet run --project MediPlatform.Write.Api/MediPlatform.Write.Api.csproj"

# 等待一下再启动下一个
Start-Sleep -Seconds 2

# 启动 SmartMedicalRAG.Api
Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$PWD'; dotnet run --project SmartMedicalRAG/src/SmartMedicalRAG.Api/SmartMedicalRAG.Api.csproj"

Write-Host ""
Write-Host "所有 API 项目已启动！" -ForegroundColor Green
Write-Host "请检查各个终端窗口的输出信息。" -ForegroundColor Yellow
Write-Host ""
Write-Host "预期的访问地址：" -ForegroundColor Cyan
Write-Host "  - MediPlatform.Read.Api: https://localhost:5001" -ForegroundColor Cyan
Write-Host "  - MediPlatform.Write.Api: https://localhost:5002" -ForegroundColor Cyan
Write-Host "  - SmartMedicalRAG.Api: https://localhost:56704" -ForegroundColor Cyan
Write-Host ""
Write-Host "Swagger UI 地址：" -ForegroundColor Cyan
Write-Host "  - MediPlatform.Read.Api: https://localhost:5001/swagger" -ForegroundColor Cyan
Write-Host "  - MediPlatform.Write.Api: https://localhost:5002/swagger" -ForegroundColor Cyan
Write-Host "  - SmartMedicalRAG.Api: https://localhost:56704/swagger" -ForegroundColor Cyan 